package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.controller.vo.BankMetaVo;
import com.dao.entity.BankMeta;
import com.service.IBankMetaService;
import com.utils.commons.ResponseBean;
import com.utils.enums.ENUM_SUCESS;

@Controller
public class BankController {
	
	@Autowired
	private IBankMetaService bankMetaService;

	@RequestMapping(value = "/tobankMeta", produces = "text/html;charset=UTF-8")
	public String tobankMeta(ModelMap map, Long bankId) {
		BankMeta bm = new BankMeta();
		if(bankId != null) {
			bm = bankMetaService.findById(bankId);
		}
		map.put("bankMeta", bm);
		return "bank/bankMeta";
	}
	
	/**
	 * 添加/修改银行信息
	 * @author geliyang
	 * @param vo
	 * @return
	 */
	@RequestMapping(value = "/addbankMeta", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addbankMeta(BankMetaVo vo) {
		try {
			bankMetaService.saveBankMeta(vo.toPo());
			return ResponseBean.of(ENUM_SUCESS.SUCESS.value);
		} catch (Exception e) {
			return ResponseBean.of(ENUM_SUCESS.FAILED.value, e.getMessage());
		}
	}
	
	@RequestMapping(value = "/getListOfBank", produces = "text/html;charset=UTF-8")
	public String getListOfBank(String search, Integer page, Integer size, ModelMap map) {
		BankMeta c = new BankMeta();
		if(page == null) {
			page = 1;
		}
		c.setBankName(search);
		Page<BankMeta> result = bankMetaService.findByCondition(c, page, size);
		map.put("list", result.getContent());
    	map.put("page", page+1);
    	map.put("search", search);
    	map.put("total",result.getTotalPages());
		return "bank/table_bankMeta";
	}
	
	@RequestMapping(value = "/deleteBank", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String deleteBank(Long deletebankid) {
		try {
			bankMetaService.deleteBank(deletebankid);
			return ResponseBean.of(ENUM_SUCESS.SUCESS.value);
		} catch (Exception e) {
			return ResponseBean.of(ENUM_SUCESS.FAILED.value, e.getMessage());
		}
	}
}
